package web.controller;

import config.SpringContextUtil;
import entity.Classes;
import entity.Student;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import service.StudentService;
import serviceImpl.ClassesServiceImpl;
import serviceImpl.StudentServiceImpl;

import java.io.IOException;
import java.util.List;

@WebServlet(name = "student",urlPatterns = "/student")
public class StudentServlet extends HttpServlet {
    private final String student_list_page = "/WEB-INF/views/instructor/instructor_student_list.jsp";
    private final String student_editView_page = "/WEB-INF/views/instructor/instructor_student_edit.jsp";
    private final String student_addView_page = "/WEB-INF/views/instructor/instructor_student_add.jsp";

    @Autowired
    private StudentService studentService;
    @Override
    public void init() throws ServletException {
        super.init();
        try {
            this.studentService = (StudentServiceImpl) SpringContextUtil.getBean("studentServiceImpl", StudentServiceImpl.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        if (action == null) {
            List<Student> students = studentService.getAllStudent();
            req.setAttribute("students", students);
            RequestDispatcher dispatcher = req.getRequestDispatcher(student_list_page);
            dispatcher.forward(req, resp);
        }else if (action.equals("viewEdit")) {
            String stuID = req.getParameter("stuID");
            req.getSession().setAttribute("stuID", stuID);
            Student student = studentService.getStudentByStuID(stuID);
            req.setAttribute("student", student);
            RequestDispatcher dispatcher = req.getRequestDispatcher(student_editView_page);
            dispatcher.forward(req, resp);
        }else if (action.equals("edit")) {
            String stuID = req.getSession().getAttribute("stuID").toString();
            String stuName = req.getParameter("stuName");
            String sex = req.getParameter("sex");
            String address = req.getParameter("address");
            String stuTel = req.getParameter("stuTel");
            String contact  = req.getParameter("contact");
            String contactTel = req.getParameter("contactTel");
            String password  = req.getParameter("password");
            Student student = new Student(stuID,stuName,sex,address,stuTel,contact,contactTel,password);
            boolean flag = studentService.updateStudent(student);
            if (flag) {
                String message = "修改成功!";
                resp.setContentType("text/html;charset=UTF-8");
                String jsCode = "<script>alert('" + message + "');window.location.href='" + req.getContextPath() + "/student';</script>";
                resp.getWriter().write(jsCode);
            }
        }else if (action.equals("delete")) {
            String stuID = req.getParameter("stuID");
            boolean flag = studentService.deleteStudent(stuID);
            if (flag) {
                String message = "删除成功!";
                resp.setContentType("text/html;charset=UTF-8");
                String jsCode = "<script>alert('" + message + "');window.location.href='" + req.getContextPath() + "/student';</script>";
                resp.getWriter().write(jsCode);
            }
        }else if (action.equals("viewAdd")) {
            RequestDispatcher dispatcher = req.getRequestDispatcher(student_addView_page);
            dispatcher.forward(req, resp);
        }else if(action.equals("add")){
            String stuID = req.getParameter("stuID");
            String classID = req.getParameter("classID");
            String stuName = req.getParameter("stuName");
            String sex = req.getParameter("sex");
            String address = req.getParameter("address");
            String stuTel = req.getParameter("stuTel");
            String contact = req.getParameter("contact");
            String contactTel = req.getParameter("contactTel");
            String password  = req.getParameter("password");
            Student student = new Student(stuID,classID,stuName,sex,address,stuTel,contact,contactTel,password);
            boolean flag = studentService.addStudent(student);
            if (flag) {
                String message = "添加成功!";
                resp.setContentType("text/html;charset=UTF-8");
                String jsCode = "<script>alert('" + message + "');window.location.href='" + req.getContextPath() + "/student';</script>";
                resp.getWriter().write(jsCode);
            }else {
                String message = "添加失败";
                resp.setContentType("text/html;charset=UTF-8");
                String jsCode = "<script>alert('" + message + "');</script>";
                resp.getWriter().write(jsCode);
            }
        }else if(action.equals("search")){
            String keyword = req.getParameter("keyword");
            List<Student> students = studentService.getStudentByStuIDORStuName(keyword,keyword);
            req.setAttribute("students", students);
            RequestDispatcher dispatcher = req.getRequestDispatcher(student_list_page);
            dispatcher.forward(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
